package com.vchello.controller.admin;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.baomidou.mybatisplus.plugins.Page;
import com.vchello.bean.bind.ProjectQuery;
import com.vchello.bean.entrepren.ProDelivery;
import com.vchello.bean.file.Folder;
import com.vchello.bean.lib.Area;
import com.vchello.bean.lib.Field;
import com.vchello.bean.lib.Round;
import com.vchello.bean.lib.Unit;
import com.vchello.bean.pro.Pro;
import com.vchello.bean.pro.ProFollow;
import com.vchello.bean.pro.ProLables;
import com.vchello.bean.pro.ProMilepost;
import com.vchello.bean.pro.ProOrganization;
import com.vchello.bean.user.User;
import com.vchello.bean.user.UserProgress;
import com.vchello.controller.BaseController;
import com.vchello.service.entrepren.ProDeliveryService;
import com.vchello.service.file.FolderService;
import com.vchello.service.lib.AreaService;
import com.vchello.service.lib.FieldService;
import com.vchello.service.lib.RoundService;
import com.vchello.service.lib.UnitService;
import com.vchello.service.project.ProjectService;
import com.vchello.service.user.UserService;
import com.vchello.util.ToolUtil;
import com.vchello.util.UploadFileHelper;

/**
 * 
 * @author power
 *
 */
@Controller
@RequestMapping(value = "/admin/project")
public class ProjectAdminController extends BaseController {

	@Autowired
	private ProjectService projectService;
	@Autowired
	private ProDeliveryService proDeliveryService;
	@Autowired
	private RoundService roundService;
	@Autowired
	private UnitService unitService;
	@Autowired
	private AreaService areaService; 
	@Autowired
	private FieldService fieldService;
	@Autowired
	private FolderService folderService;
	@Autowired
	private UserService userService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model, Page<Pro> page, ProjectQuery projectQuery) {
		page = projectService.queryPage(page, projectQuery);
		model.addAttribute("pages", page);
		return "admin/pro/proList";

	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public String listAjax(Model model, Page<Pro> page, ProjectQuery projectQuery) {
		page = projectService.queryPage(page, projectQuery);
		model.addAttribute("pages", page);
		return "admin/pro/query/proQuery";
	}
	
	/**
	 * 项目详情
	 * @return
	 */
	@RequestMapping(value = "/projectDetail/{id}", method = RequestMethod.GET)
	public String projectDetail(@PathVariable int id, Model model){
		
		Pro project = projectService.findProjectById(id);
		User user = userService.findByOne(project.getUserId());
		List<Round> roundList = roundService.findAll();
		List<Unit> unitList = unitService.findAll();
		List<Area> areaList = areaService.findAll();
		List<Field> fieldList = fieldService.findAll();
		
		//构建进度集合
		List<UserProgress> upList = projectService.userProgressesList(user.getId());
		model.addAttribute("user", user).addAttribute("project",project).addAttribute("roundList", roundList).addAttribute("fieldList", fieldList)
		.addAttribute("unitList", unitList).addAttribute("areaList", areaList).addAttribute("upList", upList);
		
		return "admin/pro/detail";
	}
	
	/**
	 * 投资笔记
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/projectNoteView/{id}", method = RequestMethod.GET)
	public String projectNoteView(@PathVariable int id, Model model){
		Pro project = projectService.findProjectById(id);
		
		User user = userService.findByOne(project.getUserId());
		
		List<Unit> unitList = unitService.findAll();		
		//标识当前项目是否是属于个人项目 0： 个人 1：机构
		int proType = 0;
		if(project.getUserId()!=user.getId()){
			User parentUser = userService.findParentUserByUserId(user.getId());
			proType=1;
			model.addAttribute("parentUser", parentUser);
		}
		int authorization = 0;
		for(ProFollow p : project.getProFollowList()){
			if(p.getUser().getId() == user.getId() || project.getUserId()==user.getId()){
				authorization = 1;
			}
		}
		if(proType==0){
			authorization = 1;
		}
		
		model.addAttribute("user", user).addAttribute("project", project).addAttribute("unitList", unitList).addAttribute("proType", proType).addAttribute("authorization", authorization);
		return "admin/pro/investment-notes";
	}
	
	/**
	 * 项目文档
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/projectFile/{id}", method = RequestMethod.GET)
	public String projectFileView(@PathVariable int id, Model model){
		
				
		
		List<Unit> unitList = unitService.findAll();		
		Pro project = projectService.findProjectById(id);
		Folder folder = folderService.findByProjectIdAndParentIdAndUserId(project.getId(), folderService.findProjectFoldByUserId(project.getUserId()).getId(), project.getUserId());
		User user = userService.findByOne(folder.getUserId());
		//标识当前项目是否是属于个人项目 0： 个人 1：机构
		int proType = 0;
		if(project.getUserId()!=user.getId()){
			User parentUser = userService.findParentUserByUserId(user.getId());
			proType=1;
			model.addAttribute("parentUser", parentUser);
		}
		int authorization = 0;
		for(ProFollow p : project.getProFollowList()){
			if(p.getUser().getId() == user.getId() || project.getUserId()==user.getId()){
				authorization = 1;
			}
		}
		if(proType==0){
			authorization = 1;
		}
		
		model.addAttribute("folder", folder).addAttribute("user", user).addAttribute("project", project)
		.addAttribute("unitList", unitList).addAttribute("proType", proType).addAttribute("authorization", authorization);
		
		return "admin/pro/project-document";
	}

	@RequestMapping(value = "/deliveryList", method = RequestMethod.GET)
	public String deliveryList(Model model, Page<ProDelivery> page, ProDelivery proDelivery) {
		page = proDeliveryService.findProDeliveryByPage(page, proDelivery);
		model.addAttribute("pages", page);
		return "admin/pro/deliveryList";
	}

	@RequestMapping(value = "/deliveryList", method = RequestMethod.POST)
	public String deliveryListAjax(Model model, Page<ProDelivery> page, ProDelivery proDelivery) {
		page = proDeliveryService.findProDeliveryByPage(page, proDelivery);
		model.addAttribute("pages", page);
		return "admin/pro/query/deliveryQuery";
	}

	@RequestMapping(value = "/updatePro", method = RequestMethod.GET)
	public void updatePro(Pro pro) {
		projectService.updatePro(pro);
	}

	/**
	 * 项目基础模块更新
	 * 
	 * @param pro
	 * @param tagsArry
	 */
	@RequestMapping(value = "/updateProject", method = RequestMethod.POST)
	public void updateProject(Pro pro, @RequestParam(value = "tagsArry[]", required = false) String[] tagsArry) {
		Pro project = projectService.findProjectById(pro.getId());

		try {
			if (null != pro.getBp() && !"".equals(pro.getBp())) {
				String bp = UploadFileHelper.isFileExists(pro.getBp(), request);
				if (ToolUtil.isEmpty(bp)) {
					returnFalse();
					return;
				}
				project.setBp(bp);
			}
			project.setName(pro.getName());
			project.setAreaId(pro.getAreaId());
			project.setProDetail(pro.getProDetail());
			project.setRoundId(pro.getRoundId());
			project.setFieldId(pro.getFieldId());
			project.setMoney(pro.getMoney());
			project.setCurrency(pro.getCurrency());
			project.setStock(pro.getStock());
			project.setLink(pro.getLink());
			boolean flag = projectService.updatePro(project);
			boolean isSuccess = false;
			if (flag) {
				// 更新跟进人
				if (projectService.deleteProlabels(pro.getId())) {
					List<ProLables> labelList = new ArrayList<ProLables>();
					if (tagsArry != null) {
						for (String labelName : tagsArry) {
							ProLables label = new ProLables(labelName, pro.getId());
							labelList.add(label);
						}
						isSuccess = projectService.saveProLabels(labelList);
					} else {
						isSuccess = true;
					}
				}
			}
			if (isSuccess) {
				returnSuccess();
			} else {
				returnFalse();
			}
		} catch (IOException e) {
			e.printStackTrace();
			returnFalse();
		}
	}
	
	/**
	 * 更新项目里程
	 * 
	 * @param proMileposts
	 */
	@RequestMapping(value = "/updateMilepost", method = RequestMethod.POST)
	public void updateMilepost(@RequestBody List<ProMilepost> proMileposts) {
		boolean flag = projectService.updateMilepost(proMileposts);
		if (flag) {
			success(proMileposts.get(0).getId() + "");
		} else {
			returnFalse();
		}
	}

	/**
	 * 更新团队人员
	 * 
	 * @param proMileposts
	 */
	@RequestMapping(value = "/updateOrganiza", method = RequestMethod.POST)
	public void updateOrganiza(@RequestBody List<ProOrganization> proOrganizations) {
		boolean flag = projectService.updateOrganizations(proOrganizations);
		if (flag) {
			success(proOrganizations.get(0).getId() + "");
		} else {
			returnFalse();
		}
	}
	
	/**
	 * 修改项目的优先级
	 * @param proId
	 * @param progressId
	 */
	@RequestMapping(value = "/changeProjectUnit" , method = RequestMethod.POST)
	public void changeProjectUnit(int proId, int unitId){
		Pro pro = projectService.findProjectById(proId);
		pro.setUnit(unitId);
		boolean flag = projectService.saveOrUpdateProject(pro);
		if(flag){
			returnSuccess();
		}else{
			returnFalse();
		}
	}

	@RequestMapping(value = "/proNote/{proId}", method = RequestMethod.GET)
	public String proNote(Model model, @PathVariable int proId) {
		Pro pro = projectService.findProjectById(proId);
		model.addAttribute("pro", pro);
		return "admin/pro/proNote";

	}
}
